package com.atguigu.gulimall.product.app;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrAttrgroupRelationVO;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 属性分组
 *
 * @author DoGH
 * @date 2023-10-25 14:29:36
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/list/{catelogId}")
    public R listCategoryId(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
        PageUtils page = attrGroupService.queryPageByCatelogId(params, catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        // 通过属性的catelogId获取属性的全路径
        attrGroup.setCatelogPath(categoryService.findCatalogPath(attrGroup.getCatelogId()));

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 获取属性分组的关联的所有属性
     *
     * @param attrgroupId 分组id
     * @return {@link R }
     * @author DuGuanghan
     * @since 2024-02-01 10:26:42
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R getRelationAttr(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> list = attrService.getRelationAttr(attrgroupId);

        return R.ok().put("data", list);
    }

    /**
     * 获取属性分组没有关联的其他属性
     *
     * @param attrgroupId 分组ID
     * @param params      参数
     * @return {@link R }
     * @author DuGuanghan
     * @since 2024-02-01 15:55:33
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getRelationNoattr(@PathVariable("attrgroupId") Long attrgroupId,
                               @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getRelationNoattr(attrgroupId, params);

        return R.ok().put("page", page);
    }

    /**
     * 删除属性与分组的关联关系
     *
     * @param relationVOS 属性-分组关系VO数组
     * @return {@link R }
     * @author DuGuanghan
     * @since 2024-02-01 11:39:46
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelationAttr(@RequestBody AttrAttrgroupRelationVO[] relationVOS) {
        attrService.deleteRelationAttr(relationVOS);

        return R.ok();
    }

    /**
     * 添加属性与分组关联关系
     *
     * @param relationVOS 属性-分组关系VO数组
     * @return {@link R }
     * @author DuGuanghan
     * @since 2024-02-02 10:15:56
     */
    @PostMapping("/attr/relation")
    public R saveRelationAttr(@RequestBody AttrAttrgroupRelationVO[] relationVOS) {
        List<AttrAttrgroupRelationEntity> entities = Arrays.stream(relationVOS).map(relationVO -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(relationVO, relationEntity);

            return relationEntity;
        }).collect(Collectors.toList());

        relationService.saveBatch(entities);

        return R.ok();
    }

    /**
     * 获取分类下所有分组&关联属性
     *
     * @param catelogId 分类 ID
     * @return {@link R }
     * @author DuGuanghan
     * @since 2024-02-26 15:53:24
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrByCatelogId(@PathVariable("catelogId") Long catelogId) {
        // 根据分类ID获得所有的属性分组信息
        List<AttrGroupEntity> attrGroupEntities = attrGroupService.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        List<AttrGroupWithAttrVO> vos = attrGroupEntities.stream().map(attrGroupEntity -> {
            AttrGroupWithAttrVO attrGroupWithAttrVO = new AttrGroupWithAttrVO();
            // 拷贝属性分组信息
            BeanUtils.copyProperties(attrGroupEntity, attrGroupWithAttrVO);

            // 根据属性分组获取属性信息
            List<AttrEntity> attrs = attrService.getRelationAttr(attrGroupWithAttrVO.getAttrGroupId());

            attrGroupWithAttrVO.setAttrs(attrs);
            return attrGroupWithAttrVO;
        }).collect(Collectors.toList());

        return R.ok().put("data", vos);
    }
}
